package com.tangqihang229350526.mynote.repository

import android.app.Activity
import android.content.Context
import android.text.TextUtils
import android.widget.Toast
import com.tangqihang229350526.mynote.database.Note
import com.tangqihang229350526.mynote.database.NoteDao
import com.tangqihang229350526.mynote.database.NoteDatabase
import com.tangqihang229350526.mynote.databinding.ActivityAddNoteBinding


class NoteRepository private constructor() : NoteDao {
    override fun query(database: NoteDatabase?, key: String?): List<Note?>? {
        val data = database?.query(
            key ?: return null
        )
        val map: MutableMap<String?, MutableList<Note>?> = HashMap()
        for (i in data!!.indices) {
            val bean = data[i]
            val type = bean.type
            var list: MutableList<Note>?
            list = map[type] ?: ArrayList()
            list.add(data[i])
            map[type] = list
        }
        val realData: MutableList<Note> = ArrayList()
        if (map.isNotEmpty()) {
            for ((key1, value) in map) {
                key1?.let {
                    realData.add(Note(it, 0))
                }
                value?.let {
                    realData.addAll(it)
                }
            }
        }
        return realData
    }

    override fun insert(
        database: NoteDatabase,
        binding: ActivityAddNoteBinding,
        img: String?,
        context: Context
    ) {
        val title = binding.etTitle.text.toString()
        val time = binding.tvTime.text.toString()
        val address = binding.etAddress.text.toString()
        val content = binding.etContent.text.toString()
        val type = binding.etType.text.toString()
        if (TextUtils.isEmpty(title)) {
            Toast.makeText(context, "请填写笔记标题", Toast.LENGTH_LONG).show()
            return
        }
        if (TextUtils.isEmpty(time)) {
            Toast.makeText(context, "请选择记录时间", Toast.LENGTH_LONG).show()
            return
        }
        if (TextUtils.isEmpty(address)) {
            Toast.makeText(context, "请填写记录地点", Toast.LENGTH_LONG).show()
            return
        }
        if (TextUtils.isEmpty(content)) {
            Toast.makeText(context, "请填写笔记内容", Toast.LENGTH_LONG).show()
            return
        }
        if (database.insertNote(title, time, address, content, type, img) == -1L) {
            Toast.makeText(context, "添加失败", Toast.LENGTH_LONG).show()
        } else {
            Toast.makeText(context, "添加成功", Toast.LENGTH_LONG).show()
            val activity = context as Activity
            activity.finish()
        }
    }

    override fun queryById(database: NoteDatabase?, key: Int): Note? {
        return database?.queryById(key)
    }

    override fun deleteById(database: NoteDatabase?,id: Int){
        if (database != null) {
            return database.deleteById(id)
        }
    }

    companion object {
        private var INSTANCE: NoteRepository? = null
        val instance: NoteRepository?
            get() {
                if (INSTANCE == null) {
                    INSTANCE = NoteRepository()
                }
                return INSTANCE
            }
    }
}
